﻿@{
    Layout = "~/Views/Shared/_LayoutMain.cshtml";
    ViewBag.Title = "网非e学院--课程购买";
    var course = ViewBag.CourseInfo as XZL.Web.Entity.Course;
    var wxpayconfig = ViewBag.Wxpayconfig as wxDemoweb.wxModel.wxHandleModel.wxPayconfig;
}

<div class="ci_Top">
    <img class="ci_courseImg" id="ci_courseImg" src="@course.CourseInfoImg" />
    <div class="ci_TopTextBG">
    </div>
    <div class="ci_TopText"><img src="~/Image/icoShiping.png" />&nbsp;&nbsp;@course.CourseName</div>
</div>
<div id="divBuy" style="line-height:40px; color:orange;font-size:16px;">
    <b>
        订单编号：@ViewBag.Ordercode <br />
        课程价格：￥@Convert.ToDouble(course.Price).ToString("0.00")<br />
    </b>
    @if (ViewBag.IsBuy < 0)
    {
        <span class="customBtn" id="payOK" style="width:45%">自用</span>
    }
    else
    {
        <span class="customBtn" id="IsBuy" style="width:45%; background-color:#ccc">已购买</span>
        <span  id="payOK" style="display:none"></span>
    }
    <span class="customBtn" id="payGive" style="width:45%;float:right">赠送</span>
</div>

<div id="divFinished" style="line-height:60px; text-align:center; color:green;font-size:16px;display:none ">
    <b> 恭喜您 课程已购买成功！</b><br />
    <span class="customBtn" onclick="document.location='../../MyInfo/MyCourse'">我的课程</span>
</div>


<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
    //底部导航选中
    $("#ImgKecheng").attr("src", "../../Image/logoKechengSel.png");

    //顶部图片高度计算
    var widthScreen = $("#ci_courseImg").width()
    $("#ci_courseImg").height(widthScreen * 11 / 18);

    //购买课程
    wx.config({
        debug: false,
        appId: "@ViewBag.AppID",   // 必填，公众号的唯一标识
        jsApiList: [  // 必填，需要使用的JS接口列表
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'getNetworkType',
            'chooseWXPay'
        ]
    });
    wx.ready(function () {
        //自用
        document.querySelector('#payOK').onclick = function () {
            wx.chooseWXPay({
                timestamp: "@wxpayconfig.timestamp", // 支付签名时间戳
                nonceStr: "@wxpayconfig.nonceStr", // 支付签名随机串
                package: "@wxpayconfig.package", // 统一支付接口返回的package包
                signType: "@wxpayconfig.signType", // 签名方式，'MD5'
                paySign: "@wxpayconfig.paySign", // 支付签名
                success: function (res) {
                    //if (res.err_msg == "get_brand_wcpay_request:ok") {
                    //    alert("支付成功！");
                    //    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    //}
                    BuyCourse(1);
                }
            });
        };

        //赠送
        document.querySelector('#payGive').onclick = function () {
            wx.chooseWXPay({
                timestamp: "@wxpayconfig.timestamp", // 支付签名时间戳
                nonceStr: "@wxpayconfig.nonceStr", // 支付签名随机串
                package: "@wxpayconfig.package", // 统一支付接口返回的package包
                signType: "@wxpayconfig.signType", // 签名方式，'MD5'
                paySign: "@wxpayconfig.paySign", // 支付签名
                success: function (res) {
                    BuyCourse(2);
                }
            });
        };

        //监听错误事件 出现'invalid signature' AJAX刷新ticket,并刷新当前页面
        //wx.error(function (res) {
        //    if (res.errMsg.indexOf("invalid signature") > 0) {
        //        alert("无效的签名，需要更新");
        //    }
        //    else {
        //        alert(res.errMsg);
        //    }
        //});
    });

    //课程购买记录
    function BuyCourse(type) {
        var orderID = "@ViewBag.Ordercode";
        var courseID = "@course.CourseID";
        var price = "@course.Price";
        var inviteID = "@ViewBag.InviteID";
        $.ajax({
            type: "Post", //提交方式
            url: "../../Course/PayCourse",//获取数据的函数
            data: { "CourseID": courseID, "OrderID": orderID, "Price": price, "Type":type}, //参数
            error: function () { },
            success: function (data) {
                if (data == "0") {
                    if (type == 1) {
                        $("#divFinished").show();
                        $("#divBuy").hide();
                    }
                    else if(type==2)
                    {
                        window.location.href = "../../Course/GiveCourse?CourseID=" + courseID + "&OrderID=" + orderID + "&InviteID=" + inviteID;
                    }
                }
            }
        });
    }

</script>